import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:proj_sfv/component/LoadingOverlay.dart';
import 'package:proj_sfv/component/MyNotification.dart';
import 'package:proj_sfv/pages/UserDetail2.dart';
import 'package:proj_sfv/domain/UserVo.dart';
import 'package:proj_sfv/http/api.dart';
import 'package:proj_sfv/http/requests.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:tencent_cloud_chat_uikit/ui/widgets/loading.dart';


class UserVoListPage extends StatefulWidget {
  @override
  State<UserVoListPage> createState() => _UserVoListPageState();

  String token;
  int id;
  String requestUrl;
  String title;
  String userName;

  UserVoListPage(this.token, this.id, this.requestUrl, this.title, this.userName);

}

class _UserVoListPageState extends State<UserVoListPage> {
  List<UserVo> data = []; // 好友列表
  var _loading = true;

  @override
  void initState() {
    getUserVoListByUrlRequest(context, widget.token, widget.requestUrl).then((value) {
      setState(() {
        data = value;
        _loading = false;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FutureBuilder(builder: (context, snapshot) {
       if (_loading) {
         return LoadingOverlay(
           isLoading: _loading,
           child: Text("正在加载"),
         );
       } else {
         return ListView.builder(
             itemCount: data.length,
             itemBuilder: (context, index) {
                return widget.id != data[index].id ? FriendItem(data.elementAt(index), () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) =>
                     UserDetailsPage2(data[index].id, widget.token, widget.id)
                  ));
                }) : FriendItem(data.elementAt(index), () {
                  Fluttertoast.showToast(msg: "你已经关注${widget.userName}");
                });
           }
         );
       }
      }),
    );
  }
}

class FriendItem extends StatelessWidget {
  final UserVo userVo;

  FriendItem(this.userVo, this._onTap);
  final _onTap;

  @override
  Widget build(BuildContext context) {
    String profile = Uri.https(cosHost, '${userVo.profile}.jpg').toString();
    return Container(
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.black12,
          width: 1
        )
      ),
      child: InkWell(
        splashColor: Colors.black38,
        onTap: _onTap,
        child: SizedBox(
          height: 80,
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
                Row(
                  children: [
                    CircleAvatar(
                      radius: 30,
                      backgroundImage: NetworkImage(profile ?? ""),
                    ),
                    SizedBox(width: 20,),
                    Column(
                      children: [
                        Text(userVo.userName?? ""),
                        Text(userVo.signature?? ""),
                      ],
                    ),
                  ],
                ),
                Align(
                  alignment: Alignment.centerRight,
                  child: Icon(userVo.gender ? Icons.man: Icons.woman),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }


}